<?php

$userid = get_loggedin_userid();
$client_id = 'opinions';
$offset = get_input('offset');
$limit = 15;

$get_friends = greengar_get_friends($userid, OPINIONS);
$friends_array = array();
foreach($get_friends as $friends) {
	$friends_array[] = userToArray($friends);
}

$fetch_array = array();
$i = 0;
if(count($friends_array) > 0) {

	$friends_id_string = '(';
	foreach($friends_array as $friends) {
		$friends_id_string .= $friends['guid'] . ',';
	}
	
	if(strlen($friends_id_string)>1) $friends_id_string = substr($friends_id_string, 0, -1);
	$friends_id_string .= ')';
	
	$select_sql = "SELECT * FROM users_action WHERE action <> 'add_friend' AND subject_id IN $friends_id_string ORDER BY id DESC LIMIT $offset, $limit";
	$fetch_data = execute_query($select_sql, get_db_link('readwrite'));

	$i = 0;
	while($row = mysql_fetch_object($fetch_data)) {
		
		// action
		$fetch_array[$i]['action'] = $row->action;
		
		// subject
		$userEntity = get_entity($row->subject_id);
		$fetch_array[$i]['user_subject'] = userToArray($userEntity, $client_id, FALSE);
		
		// object_one (rating)
		if($row->action=='add_interest' || $row->action=='add_rating' || $row->action=='add_comment') {
			$object_one = get_entity($row->object_one_id);
			$fetch_array[$i]['object_one'] = $object_one->toArray();
			$fetch_array[$i]['count_comment'] = greengar_opinions_count_interestRatingComment($row->object_one_id, NULL);
		}
		
		// object_two (add_interest, add_rating: interest object, add_comment: user object)
		if($row->action=='add_interest' || $row->action=='add_rating') {
			$object_two = get_entity($row->object_two_id);
			$fetch_array[$i]['object_two'] = $object_two->toArray();
		} else if($row->action=='add_comment') {
			$object_two = get_entity($row->object_two_id);
			$fetch_array[$i]['object_two'] = userToArray($object_two, $client_id, FALSE);
		}
		
		// action_metadata
		$fetch_array[$i]['action_metadata'] = $row->action_metadata;
		
		// created time
		$fetch_array[$i]['created_time'] = $row->created_time;
		
		$i++;
		
	}	
}

echo json_encode(export_result(200, $fetch_array));
?>